package com.lgx.mall.web.controller;

import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.lgx.common.entity.IntraCity;
import com.lgx.common.service.IIntraCityService;
import com.lgx.common.web.Constants;
import com.lgx.right.entity.Organization;
import com.lgx.right.service.IOrganizationService;

/**
 * 
 * 
 * @author acer
 *
 */
@Controller
@RequestMapping("/map")
public class BusinessMapController {
	@Autowired
	private IOrganizationService organizationService;

	public IOrganizationService getOrganizationService() {
		return organizationService;
	}

	public void setOrganizationService(IOrganizationService organizationService) {
		this.organizationService = organizationService;
	}

	@Autowired
	private IIntraCityService intraCityService;
	
	public IIntraCityService getIntraCityService() {
		return intraCityService;
	}

	public void setIntraCityService(IIntraCityService intraCityService) {
		this.intraCityService = intraCityService;
	}

	@RequestMapping("/index.htm")
	public String index(HttpServletRequest request,HttpServletResponse response){
		
		return "map/index";
	}
	
	@RequestMapping("/head.ftl")
	public String head(HttpServletRequest request,HttpServletResponse response){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		request.setAttribute("basePath", basePath);
		
		return "map/head";
	}
	
	@RequestMapping("/map.ftl")
	public String map(HttpServletRequest request,HttpServletResponse response){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		request.setAttribute("basePath", basePath);
		
		String ip = request.getRemoteAddr();
		
		IntraCity intraCity = (IntraCity) request.getSession().getAttribute(Constants.INTRA_CITY_SIGN);
				
		URL url = Constants.class.getResource("");
		File file = null;
		try {
			file = new File(url.toURI());
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
		
		if(file != null){
			//根据IP获取当前访问所在城市
			intraCity = this.intraCityService.findIntraCityByIP(ip,Constants.INTRA_CITY_DATA_NAME, file.getAbsolutePath());
		}
		
		String areaNode = "";
		if(intraCity != null){
			areaNode = intraCity.getAreaId();
		}
		List<Organization> organizationLists = this.organizationService.findOrganizationListByareaNode(areaNode);
		request.setAttribute("organizationLists", organizationLists);
		return "map/map";
	}
}
